package edu.hunan.cxb18.controller;

import edu.hunan.cxb18.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import java.util.Objects;

/**
 * @author AuthorName
 * @create 2020-04-08 9:30
 */
@Controller
@RequestMapping("/user")
//seiionAttributes将model中对应的数据注入到session中
@SessionAttributes(value = {"users"},types = {User.class})
public class UserController {
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String test(User user, Model model){
        System.out.println(user.getPwd()+user.getUsername());
        if(!Objects.isNull(user)&&"250".equals(user.getPwd())){
            System.out.println("success");
            model.addAttribute("users",user);
            return "redirect:loginList.html";
        }
        model.addAttribute("errorMessage","密码错误");
        return "login";
    }
    @GetMapping(value = "/logout")
    public String logout(SessionStatus sessionStatus){
        System.out.println("清除session中的数据");
        sessionStatus.setComplete();
        return "login";
    }
}
